package com.smyh.UIM.device;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.smyh.UIM.R;
import com.smyh.bean.DeviceParam5;
import com.smyh.bean.DeviceParam6;
import com.smyh.cache.DeviceParamInfoCache;
import com.smyh.cache.UIM.MainCahce;
import com.smyh.commcache.commd.UIParamOptionUtil;
import com.smyh.common.MinaClientThread;
import com.smyh.protcol.device.CMDUtil;
import com.smyh.util.HexStrUtil;

import org.apache.mina.core.buffer.IoBuffer;

/**
 * Created by Administrator on 2016/7/27.
 */
public class DeviceParam06Fm extends Fragment implements View.OnClickListener {
    private Button param6ReadBtn;
    private static TextView waveDealSW,elecDirectSamll,voltDirectSamll,beforeFaultWaveCount
        //rf参数设置，中级功能开关
            ,afterFaultWaveCount,sampleCycleCheck,elecRalte1,elecRalte2
            ,voltRalte1,voltRalte2;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_device_param06,container,false);
        initView(view);
        initEvent();
        return view;
    }
    private void initEvent() {
        waveDealSW.setOnClickListener(this);
        elecDirectSamll.setOnClickListener(this);
        voltDirectSamll.setOnClickListener(this);
        beforeFaultWaveCount.setOnClickListener(this);
        afterFaultWaveCount.setOnClickListener(this);
        sampleCycleCheck.setOnClickListener(this);
        elecRalte1.setOnClickListener(this);
        elecRalte2.setOnClickListener(this);
        voltRalte1.setOnClickListener(this);
        voltRalte2.setOnClickListener(this);
        param6ReadBtn.setOnClickListener(btnClickListenrer);
    }
    private View.OnClickListener btnClickListenrer = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.param6ReadBtn:{
                    if(MinaClientThread.session == null){
                        Toast.makeText(getContext(),"请连接wifi！",Toast.LENGTH_SHORT).show();
                        return;
                    }
                    IoBuffer buf = CMDUtil.askOutParam(HexStrUtil.hexStr2Bytes(MainCahce.chooseDeviceAddr), (byte) 6);
                    MinaClientThread.session.write(buf);
                    Toast.makeText(getContext(),"读取数据！",Toast.LENGTH_SHORT).show();
                    break;
                }
                default:{
                    break;
                }
            }
        }
    };
    private void initView(View view) {
        waveDealSW = (TextView) view.findViewById(R.id.waveDealSWText);
        elecDirectSamll = (TextView) view.findViewById(R.id.elecDirectSamll);
        voltDirectSamll = (TextView) view.findViewById(R.id.voltDirectSamll);
        beforeFaultWaveCount = (TextView) view.findViewById(R.id.beforeFaultWaveCount);
        afterFaultWaveCount = (TextView) view.findViewById(R.id.afterFaultWaveCount);
        sampleCycleCheck = (TextView) view.findViewById(R.id.sampleCycleCheck);
        elecRalte1 = (TextView) view.findViewById(R.id.elecRalte1);
        elecRalte2 = (TextView) view.findViewById(R.id.elecRalte2);
        voltRalte1 = (TextView) view.findViewById(R.id.voltRalte1);
        voltRalte2 = (TextView) view.findViewById(R.id.voltRalte2);

        param6ReadBtn = (Button) view.findViewById(R.id.param6ReadBtn);
    }

    static Handler handler1 = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            DeviceParam6 obj = DeviceParamInfoCache.getDeviceParamMap6().get(MainCahce.chooseDeviceAddr);
            if (obj != null){
                waveDealSW.setText(""+obj.getWaveSwitch());
                elecDirectSamll.setText(""+obj.getElecDCBranch());
                voltDirectSamll.setText(""+obj.getVoltDCBranch());
                beforeFaultWaveCount.setText(""+obj.getBeforeFaultWaveCount());
                afterFaultWaveCount.setText(""+obj.getAfterFaultWaveCount());
                sampleCycleCheck.setText(""+obj.getSmapleCycleCheck());
                elecRalte1.setText(""+obj.getElecRatio_1());
                elecRalte2.setText(""+obj.getElecRatio_2());
                voltRalte1.setText(""+obj.getVoltRatio1());
                voltRalte2.setText(""+obj.getVoltRatio2());

            }else {
                waveDealSW.setText(" ");
                elecDirectSamll.setText(" ");
                voltDirectSamll.setText(" ");
                beforeFaultWaveCount.setText(" ");
                afterFaultWaveCount.setText(" ");
                sampleCycleCheck.setText(" ");
                elecRalte1.setText(" ");
                elecRalte2.setText(" ");
                voltRalte1.setText(" ");
                voltRalte2.setText(" ");

            }
        }
    };
    public static void freshDeviceParam6Model() {
        handler1.sendMessage(new Message());
    }
    int index = 1;
    String str = "";
    @Override
    public void onClick(View v) {
        if(MinaClientThread.session==null){
            Toast.makeText(getContext(), "请连接wifi", Toast.LENGTH_SHORT).show();
            return;
        }
        if(MainCahce.chooseDeviceAddr.equals("")){
            Toast.makeText(getContext(), "请选择设备", Toast.LENGTH_SHORT).show();
            return;
        }
        switch (v.getId()){
            case R.id.waveDealSWText:{
                index = 1;
                str = waveDealSW.getText().toString();
                break;
            }
            case R.id.elecDirectSamll:{
                index = 2;
                str = elecDirectSamll.getText().toString();
                break;
            }
            case R.id.voltDirectSamll:{

                index = 3;
                str = voltDirectSamll.getText().toString();
                break;
            }
            case R.id.beforeFaultWaveCount:{

                index = 4;
                str = beforeFaultWaveCount.getText().toString();
                break;
            }
            case R.id.afterFaultWaveCount:{
                index = 5;
                str = afterFaultWaveCount.getText().toString();
                break;
            }
            case R.id.sampleCycleCheck:{
                index = 6;
                str = sampleCycleCheck.getText().toString();
                break;
            }
            case R.id.elecRalte1:{
                index = 7;
                str = elecRalte1.getText().toString();
                break;
            }
            case R.id.elecRalte2:{
                index = 8;
                str = elecRalte2.getText().toString();
                break;
            }
            case R.id.voltRalte1:{
                index = 9;
                str = voltRalte1.getText().toString();
                break;
            }
            case R.id.voltRalte2:{
                index = 10;
                str = voltRalte2.getText().toString();
                break;
            }
        }
        final EditText edit = new EditText(getContext());
        edit.setText(str);
        edit.setSelection(str.length());
        new AlertDialog.Builder(getContext()).setTitle("请输入").setView(edit)
                .setPositiveButton("修改", new DialogInterface.OnClickListener(){
            @Override
            public void onClick(DialogInterface dialog, int which) {
                try {
                    byte[] addrs = HexStrUtil.hexStr2Bytes(MainCahce.chooseDeviceAddr);
                    IoBuffer buffer = UIParamOptionUtil.writeDeviceParm(addrs,6,index,Integer.valueOf(edit.getText().toString()));
                    MinaClientThread.session.write(buffer);
                }catch (Exception e){
                    Toast.makeText(getContext(), "格式不正确", Toast.LENGTH_SHORT).show();
                }
            }
        }) .setNegativeButton("取消", null).show();
    }

}
